package com.runtime;

import com.base.Constant;
import com.base.Sprite;
import com.util.ImageUtil;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public class Bird extends Sprite {

    private Image image;

    private int[] clipX = {9, 9 + 34 + 18, 9 + 34 + 18 + 34 + 18};
    private int clipY = 10;

    public Bird(int x, int y) {
        init(x, y);
    }

    private void init(int x, int y) {
        image = ImageUtil.get("birds");
        setX(x);
        setY(y);
        offsetY = getY();
        setWidth(34);
        setHeight(24);
    }

    private int index;

    @Override
    public void draw(Graphics g) {
        g.drawImage(image, getX(), getY(), getX() + getWidth(), getY() + getHeight(),
                clipX[index / 5], clipY, clipX[index / 5] + getWidth(), clipY + getHeight(), null);
        index++;
        if (index == 15) {
            index = 0;
        }
    }

    private static final double G = 0.4;
    private int time = 0;

    private int offsetY;

    @Override
    public void move() {
        setY((int) (offsetY + (G * time * (time - 37) / 2)));
        time++;
    }

    public void resetY() {
        offsetY = getY();
        time = 0;
    }

    public void resetSelf() {
        setY(Constant.WINDOW_HEIGHT / 2);
        offsetY = getY();
        time = 0;
    }


    public Rectangle createRectangle() {
        return new Rectangle(getX(), getY(), getWidth(), getHeight());
    }
}
